<?php

require_once("header.php");

$smarty->assign('topicList', getTopicListCode());

$topic = $_GET['topic'];
$keyword = $_GET['keyword'];

if ( !preg_match("/^-?[0-9]$/", $topic) ) {
	$topic = 0;
}

// generate search sql
if ( $keyword != '' ) {
	$sql = "Select * From post Where content like '%".addslashes($keyword)."%' Order by post_time desc";
} elseif ( $topic == 0 || $topic == '' ) {
	$sql = 'Select * From post Order by post_time DESC Limit 10';
} elseif ( $topic == -1 && $_SESSION['logon'] === true ) {
	// my favourite
	$sql = "Select * From post Where post_id In (Select post_id From favourite Where user_id = ".$_SESSION['user_id']." Order by favourite_time desc)";
} elseif ( $topic == -2 && $_SESSION['logon'] === true ) {
	// my posts
	$sql = "Select * From post Where user_id = ".$_SESSION['user_id']." Order by post_time desc";
} elseif ( $topic == -3 ) {
	// top rates
	$sql = "Select * From post Where post_id In (Select post_id From vote Group By post_id Order By Sum(result) DESC) Limit 10";
} else {
	$sql = "Select * From post Where topic_id = $topic Order by post_time desc";
}

$results = array();

$result = $db->query($sql);
$postNo = 0;

foreach ( $result as $row ) {

	// all vote number
	$postNo++;
	$all = 0;
	$all_r = $db->query("SELECT Count(*) As Total FROM vote WHERE post_id = ".$row['post_id']);
	if ( $all_r ) {
		foreach ( $all_r as $r ) {
			$all = $r['Total'];
			break;
		}
	}
	
	if ( $all > 0 ) {
	
		// support vote number
		$support = 0;
		$support_r = $db->query("SELECT Count(*) As Support FROM vote WHERE result = 1 And post_id = ".$row['post_id']);
		if ( $support_r ) {
			foreach ( $support_r as $r ) {
				$support = $r['Support'];
				break;
			}
		}
		
		// current user existing vote 
		$vote = 0;
		if ( $_SESSION['logon'] === true ) {
			$vote_r = $db->query("SELECT result FROM vote WHERE user_id = ".$_SESSION['user_id']." And post_id = ".$row['post_id']);
			if ( $vote_r ) {
				foreach ( $vote_r as $r ) {
					$vote = $r['result'];
					break;
				}
			}
		}
		
	}
	
	$rate = ( $all == 0 ? "50%" : round($support / $all * 100)."%" );
	
	$favourite = '';
	// check if add to favourite
	if ( $_SESSION['logon'] === true ) {
		if ( $topic == -1 || count( $db->query("SELECT post_id FROM favourite WHERE user_id = ".$_SESSION['user_id']." And post_id = ".$row['post_id'])->fetchAll() ) > 0 ) {
			$favourite = 'checked="checked"';
		}
	}
	
	$results[] = array(
		'topicid' => $row['topic_id'],
		'postid' => $row['post_id'],
		'content' => htmlspecialchars(stripslashes($row['content'])),
		'posttime' => $row['post_time'],
		'userid' => $row['user_id'],
		'rate' => $rate,
		'pro' => ( $vote > 0 ? " voted" : "" ),
		'can' => ( $vote < 0 ? " voted" : "" ),
		'favourite' => $favourite
	);

}

$nopost = ($postNo > 0 ? '' : '<div class="block">No post match your search.</div>');

$smarty->assign('nopost', $nopost);

$smarty->assign('results', $results);

if ( $_SESSION['logon'] === true ) {

	$smarty->assign('topicOption', getTopicOptionCode());
	$smarty->display('logon.html');
	
} else {

	$smarty->assign('countryList', getCountryListCode());
	$smarty->display('index.html');
	
}



?>